"""

@File:order_setting_service.py
@Datetime:2022/11/6 22:44
@Author:wangt
@Desc:
"""
import json

from api.base_service import BaseService


class OrderSettingService(BaseService):

    def __init__(self):
        super().__init__()
        self.service_name = "OrderSettingService"

    def add(self,data):
        """
        功能：添加预约设置
        :param data: 传入列表类型数据，元素为字典
        如：[{"orderDate": "2023-02-06 16:45:12", "number": 50}]
        :return:返回True或False
        """
        resp = self.dubbo_client.invoke(self.service_name, "add", data)
        if resp == "null":
            return True
        else:
            return False

    def get_order_setting_by_month(self,month):
        """
        功能：按月统计预约设置
        :param month: 传入月份数据，如："2021-03"
        :return: 成功返回列表数据，失败返回None
        """
        resp = self.dubbo_client.invoke(self.service_name, "getOrderSettingByMonth", month)
        if "Failed" in resp:
            return None
        else:
            # json.loads()作用为将返回结果还原为字典或列表格式数据
            return json.loads(resp)


    def edit_number_by_date(self,data):
        """
        功能：根据日期修改预约设置
        :param data: 传入日期 orderDate，和修改数量 number参数
        如：{"orderDate":"2023-02-05 21:04:33","number":35}
        :return:返回True或False
        """
        data["class"] = "com.itheima.pojo.OrderSetting"
        resp = self.dubbo_client.invoke(self.service_name,"editNumberByDate",data)
        if resp == "null":
            return True
        else:
            return False


if __name__ == '__main__':
    oss = OrderSettingService()

    # data = [{"orderDate": "2023-02-05 16:45:12", "number": 45}]
    # resp = oss.add(data)

    # month = "2021-02"
    # resp = oss.get_order_setting_by_month(month)

    data = {"orderDate":"2023-02-05 21:04:33","number":66}
    resp = oss.edit_number_by_date(data)
    print(resp,type(resp))